<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>投注收益计算器</title>
    <!-- 引入 Vue 的 CDN -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
    <div id="app">
        <h1>投注收益计算器</h1>

        <div>
            <label for="totalMoney">总资金（元）:</label>
            <input type="number" v-model.number="totalMoney" id="totalMoney" min="1">
        </div>

        <div>
            <label for="odds">赔率:</label>
            <input type="number" v-model.number="odds" id="odds" step="0.01" min="1.01">
        </div>

        <div>
            <label for="initialBet">起始投注金额（元）:</label>
            <input type="number" v-model.number="initialBet" id="initialBet" min="1">
        </div>

        <div>
            <label for="targetProfitRate">目标收益率（%）【针对上次的累积,不是利润.这个容易计算】:</label>
            <input type="number" v-model.number="targetProfitRate" id="targetProfitRate" min="1" step="0.1">
        </div>

        <div>
            <label for="targetProfitAmount">每次目标利润（元）:</label>
            <input type="number" v-model.number="targetProfitAmount" id="targetProfitAmount" min="1">
        </div>

        <div>
            <label for="rounds">最大追投次数:</label>
            <input type="number" v-model.number="maxRounds" id="rounds" min="1">
        </div>

        <hr>

        <button @click="calculateBets">开始计算</button>

        <h2>追注详情</h2>
        <table v-if="betRecords.length > 0">
            <thead>
                <tr>
                    <th>轮次</th>
                    <th>投入金额（元）</th>
                    <th>预计收益（元）</th>
                    <th>利润（元）</th>
                    <th>利润（%）</th>
                    <th>累计投入（元）</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(record, index) in betRecords" :key="index">
                    <td>{{ index + 1 }}</td>
                    <td>{{ record.betAmount }}</td>
                    <td>{{ record.expectedProfit.toFixed(2) }}</td>
                    <td>{{ record.profit.toFixed(2) }}</td>
                    <td>{{  ((record.profit/record.totalInvested ) * 100).toFixed(1) }} %</td>
                    <td>{{ record.totalInvested.toFixed(2) }}</td>
                </tr>
            </tbody>
        </table>

        <p v-else>请填写所有的输入参数，然后点击“开始计算”</p>
    </div>

    <script>
        new Vue({
            el: '#app',
            data: {
                totalMoney: 1000,         // 总资金
                odds: 1.9,                // 赔率
                initialBet: 100,           // 起始投注金额
                targetProfitRate: 12,      // 目标收益率（百分比）
                targetProfitAmount: 200,   // 每次目标利润（元）
                maxRounds: 5,              // 最大追投次数
                betRecords: []             // 追注记录
            },
            methods: {
                calculateBets() {
                    this.betRecords = [];  // 清空之前的记录
                    let currentBet = Math.ceil(this.initialBet);  // 取整处理
                    let totalInvested = 0;
                    let success = false;

                    for (let round = 1; round <= this.maxRounds; round++) {
                        // 计算累计投入
                        totalInvested += currentBet;
                        
                        // 计算每次的目标收益
                        let expectedProfit = currentBet * this.odds;
                        let profit = expectedProfit - totalInvested;

                        // 如果利润超过设定的目标利润元，则使用目标利润元进行计算
                        if (profit > this.targetProfitAmount) {
                            profit = this.targetProfitAmount;
                            expectedProfit = totalInvested + profit;  // 重新计算预计收益
                        }

                        // 记录每轮的下注情况
                        this.betRecords.push({
                            betAmount: currentBet,
                            expectedProfit: expectedProfit,
                            profit: profit,
                            totalInvested: totalInvested
                        });

                        // 判断是否达到了目标收益率
                        if (profit / totalInvested * 100 >= this.targetProfitRate) {
                            success = true;
                            // break;  // 达到目标收益，退出循环
                        }

                        // 输掉的情况下，增加投注金额进行追投（取整处理）
                        currentBet = Math.ceil((totalInvested * (1 + this.targetProfitRate / 100)) / (this.odds - 1));
                        
                        // 如果下次追投的盈利超过目标收益元，重新计算投注金额以满足盈利限制
                        let nextProfit = (currentBet * this.odds) - (totalInvested + currentBet);
                        if (nextProfit > this.targetProfitAmount) {
                            currentBet = Math.ceil(  (totalInvested + this.targetProfitAmount) / (this.odds - 1));
                        }

                        // 如果投注金额超过总资金，停止计算
                        if (totalInvested + currentBet > this.totalMoney) {
                            break;
                        }
                    }
                }
            }
        });
    </script>
</body>
</html>
